/*
 ============================================================================
 Name        : ConsolaPPD.c
 Author      : 
 Version     :
 Copyright   : Your copyright notice
 Description : Hello World in C, Ansi-style
 ============================================================================
 */

#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdint.h>
#include <sys/un.h>
#include "nipcManager.h"
#include "interfaz.h"
#include "config.h"

#define NSTRS       3           /* no. of strings  */
//#define ADDRESS     "/home/utn_so/Escritorio/PPD/workspace1/tpPPD/mysocket"  /* addr to connect */

/*
 * Strings we send to the server.
 */

void error(const char *);

int32_t main(int argc,char**argv)
//int32_t main()
{
   int32_t sockfd, servlen;
   struct sockaddr_un  serv_addr;
   char dir[100];
   char *ADDRESS = malloc(200);


   loadConfig(ADDRESS,"configConsola.txt");


   bzero((char *)&serv_addr,sizeof(serv_addr));
   serv_addr.sun_family = AF_UNIX;
   strcpy(serv_addr.sun_path, ADDRESS);
   servlen = strlen(serv_addr.sun_path) +
                 sizeof(serv_addr.sun_family);
   if ((sockfd = socket(AF_UNIX, SOCK_STREAM,0)) < 0)
       error("Creating socket");
   if (connect(sockfd, (struct sockaddr *)
                         &serv_addr, servlen) < 0)
       error("Connecting");
   printf("Ingrese la operacion que desea:");
   bzero(dir,100);


	char command[20];
	int32_t cantS,nbytes;
	char param[80];
	PAQUETE_NIPC_CABECERA *cabecera = (PAQUETE_NIPC_CABECERA *)malloc(sizeof(PAQUETE_NIPC_CABECERA));

	while(1)
	{
		gets(dir);
		cantS = sscanf(dir, "%s %s", command, param);

		if (strstr(command, "info"))
		{

			char *paq = serializarNipc(consolaINFO,"",0);
			send(sockfd,paq,sizeof(PAQUETE_NIPC_CABECERA),0);
			free(paq);
			nbytes = recv(sockfd,cabecera,sizeof(PAQUETE_NIPC_CABECERA),0);

			if (nbytes > 0)
			{
				char *posActual = (char*) malloc(cabecera->payloadlength);
				nbytes = recv(sockfd,posActual,cabecera->payloadlength,0);
				if (nbytes > 0)
				{
					printf("\n%s \n",posActual);
				}
				free(posActual);
			}
		}
		else if (strstr(command, "clean"))
		{
			strcat(param,"\0");

			int32_t payloadlength = strlen(param) +1;
			char *paq = serializarNipc(consolaCLEAN,param,payloadlength);
			send(sockfd,paq,sizeof(PAQUETE_NIPC_CABECERA) + payloadlength,0);
			free(paq);
			nbytes = recv(sockfd,cabecera,sizeof(PAQUETE_NIPC_CABECERA),0);

			if (nbytes > 0)
			{
				char *msj = (char*) malloc(cabecera->payloadlength);
				nbytes = recv(sockfd,msj,cabecera->payloadlength,0);
				if (nbytes > 0)
				{
					printf("\n%s \n",msj);
				}
				free(msj);
			}
		}
		else if (strstr(command, "trace"))
		{
			strcat(param,"\0");

			int32_t payloadlength = strlen(param) +1;
			char *paq = serializarNipc(consolaTRACE,param,payloadlength);
			send(sockfd,paq,sizeof(PAQUETE_NIPC_CABECERA) + payloadlength,0);
			free(paq);


			int32_t respuestasArecibir = 1;
			char *dir = strtok(param,"-");
			while((dir=strtok(NULL,"-")) != NULL)
			{
				respuestasArecibir++;
			}
			int32_t i;
			for (i=0;i<respuestasArecibir;i++)
			{
				nbytes = recv(sockfd,cabecera,sizeof(PAQUETE_NIPC_CABECERA),0);

				if (nbytes > 0)
				{
					char *msj = (char*) malloc(cabecera->payloadlength);
					nbytes = recv(sockfd,msj,cabecera->payloadlength,0);
					if (nbytes > 0)
					{
						printf("\n%s \n",msj);
					}
					free(msj);
				}
			}
		}

		printf("Ingrese la operacion que desea:");

	}
   close(sockfd);

   return 0;
}

void error(const char *msg)
{
    perror(msg);
    exit(0);
}
